package com.seari.groovyrunner.service.impl;

import com.seari.groovyrunner.bean.GroovyLog;
import com.seari.groovyrunner.bean.ReturnBean;
import com.seari.groovyrunner.mapper.GroovyLogMapper;
import com.seari.groovyrunner.service.GroovyLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("GroovyLogServiceImpl")
public class GroovyLogServiceImpl implements GroovyLogService {

    @Autowired
    private GroovyLogMapper mapper;

    @Override
    public List<GroovyLog> getLogListByService(String service, String state, String page, String limit) {
        Integer startIndex = (Integer.valueOf(page) - 1) * Integer.valueOf(limit);
        return mapper.getGroovyLogListByService(service,state,startIndex,Integer.valueOf(limit));
    }

    @Override
    public int getLogCountByName(String service) {
        return mapper.getLogCountByName(service);
    }

    @Override
    public void deleteGroovyLogByTime(long date) {
        mapper.deleteGroovyLogByTime(getSDFTime(date));
    }

    @Override
    public void addGroovyLog(String modul, String type, String desc, String requestURI, String className, String params, ReturnBean keys) {
        GroovyLog groovyLog=new GroovyLog();
        groovyLog.setGroovyService(requestURI);
        groovyLog.setResult(keys.getData());
        groovyLog.setCode(keys.getCode());
        groovyLog.setMsg(keys.getMsg());
        groovyLog.setLogTime(getSDFTime(System.currentTimeMillis()));

        groovyLog.setLogModul(modul);
        groovyLog.setLogType(type);
        groovyLog.setLogDesc(desc);
        groovyLog.setClassName(className);
        groovyLog.setParams(params);
        mapper.addGroovyLog(groovyLog);
    }

    private String getSDFTime(long time){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(time));
    }
}
